<?php
namespace app\webqun\controller;
use wechat\MchApi;
use think\db\Expression;

class NotifyController extends \think\Controller {
  /**
   *  @brief  : 微信`统一下单`支付成功通知
   *  @return : XML
   *  @details: 
   *  https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7&index=8
   */
  public function WxPayAction() {
    trace(__METHOD__);
    $data = $this->request->getInput();
    trace($data);
    if (empty($data)) {
      return;
    }
    $data = MchApi::XML2Array($data);
    ////////////////////////////////////////////////////////////
    $host = $this->request->host();
    $eax = explode('.', $host);
    $eax = $eax[1];
    $conf = config("wxgzh.{$eax}");
    $mapi = new MchApi($conf);
    ////////////////////////////////////////////////////////////
    $sign1 = $data['sign'] ?? '';
    $sign2 = $mapi->MakeSign($data);
    trace("sign1={$sign1}");
    trace("sign2={$sign2}");
    if ($sign1 != $sign2) {
      trace('数据验签失败');
      return;
    }
    trace('数据验签成功');
    ////////////////////////////////////////////////////////////
    $attach = json_decode($data['attach'] ?? '', true);
    ////////////////////////////////////////////////////////////
    $Card = [];
    $CardID = $attach['CardID'] ?? 0;
    if ($CardID > 0) {
      $Card = table('CardCoin', 'rydb.websms')->where(['State' => 1, 'ID' => $CardID])->find();
    }
    if (empty($Card)) {
      trace('attach携带的CardID无效');
      return;
    }
    ////////////////////////////////////////////////////////////
    $UserID = $attach['UserID'] ?? 0;
    if ($UserID <= 0) {
      trace('attach携带的UserID无效');
      return;
    }
    ////////////////////////////////////////////////////////////
    $OrderID = table('ChargeOnline', 'rydb.websms')->where([
      'UserID' => $UserID, 'MerBillNo' => $data['out_trade_no'], 'Status' => 0, 'GameStatus' => 0
    ])->order(['ID'=>'DESC'])->value('ID');
    if ($OrderID <= 0) {
      trace("订单[{$data['out_trade_no']}]找不到或已完成");
      return;
    }
    ////////////////////////////////////////////////////////////
    // 记录账变信息
    $oldRoomCard = table('UserRoomCard', 'rydb.treasure')->where('UserID', $UserID)->value('RoomCard');
    $eax = table('CostRoomCardRecord', 'rydb.record')->insert([
      'dwUserID'      => $UserID,
      'dwCost'        => $Card['Coin'] + $Card['Zeng'],
      'dwCostType'    => 10,
      'beleftRoomCard'=> $oldRoomCard,
      'describe'      => sprintf('冲值%.02f元，获得钻石%d张，剩余钻石%d张',
        $data['cash_fee'] / 100,
        $Card['Coin'] + $Card['Zeng'],
        $Card['Coin'] + $Card['Zeng'] + $oldRoomCard
      ),
      'CostTime'      => new Expression('GETDATE()'),
    ]);
    // 增加用户房卡
    $eax = table('UserRoomCard', 'rydb.treasure')->where('UserID', $UserID)->setInc('RoomCard', $Card['Coin'] + $Card['Zeng']);
    // 更新订单状态
    $eax = table('ChargeOnline', 'rydb.websms')->where('ID', $OrderID)->update([
      'Updatetime'  => new Expression('GETDATE()'),
      'Status'      => 1,
      'GameStatus'  => 1,
      'TradeNo'     => $data['transaction_id']??'',
    ]);
    ////////////////////////////////////////////////////////////
    return MchApi::Array2XML(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
  }

}
